﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MuzoBlog.Core.BussinessLogic.Languages;
using System.IO;
using System.Web;

namespace MuzoBlog.Core.BussinessLogic.Themes
{
    public abstract class ThemeManager
    {
        // TODO: Cache
        /// <summary>
        /// Physical path of info.xml file
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static Theme GetInfo(string fullPath)
        {
            Theme t = new Theme();
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(fullPath);
                t.Author = ds.Tables[0].Rows[0]["author"].ToString();
                t.Name = ds.Tables[0].Rows[0]["name"].ToString();
                t.Version = ds.Tables[0].Rows[0]["version"].ToString();
                t.WebSite = ds.Tables[0].Rows[0]["website"].ToString();
            }
            catch (Exception exception)
            {
                throw new Exception(LanguageManager.Get("ThemeNotLoaded"), exception);
            }

            return t;
        }

        // TODO: Cache
        /// <summary>
        /// Physical path of info.xml file
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static List<Theme> GetThemes()
        {
            List<Theme> themes = new List<Theme>();
            string fullPath;
            DataSet ds;
            Theme t;

            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Themes/"));
                foreach (var item in directoryInfo.GetDirectories())
                {
                    fullPath = Path.Combine(item.FullName, "Info.xml");
                    if (!File.Exists(fullPath))
                        continue;
                    
                    ds = new DataSet();
                    ds.ReadXml(fullPath);

                    t = new Theme();
                    t.Author = ds.Tables[0].Rows[0]["author"].ToString();
                    t.Name = ds.Tables[0].Rows[0]["name"].ToString();
                    t.Version = ds.Tables[0].Rows[0]["version"].ToString();
                    t.WebSite = ds.Tables[0].Rows[0]["website"].ToString();

                    themes.Add(t);
                }
            }
            catch (Exception exception)
            {
                throw new Exception(LanguageManager.Get("ThemeNotLoaded"), exception);
            }

            return themes;
        }
    }
}